package com.ymatou.iapi.sellerproduct.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.service.AttachProductToLivesCall;
import com.ymatou.iapi.sellerproduct.service.BatchDeleteProductsCall;
import com.ymatou.iapi.sellerproduct.service.RemoveProductFromLiveCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.YmtProductsInLive;
import com.ymttest.database.mongo.LiveProductModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.mongo.jproduct.LiveProductsWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;


public class Ts_RemoveProductFromLive {	
	private static RemoveProductFromLiveCall removeProductFromLiveCall = new RemoveProductFromLiveCall();
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商品移出直播");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(removeProductFromLiveCall.getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RemoveProductFromLive_001() {
		Logger.start(true, "商品移出直播");
		Logger.comment("Test in Ts_AddProductInLives->Tc_AddProductInLives");
	
	}

	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RemoveProductFromLive_002() {
		Logger.start(true,"校验首次露出时间-listing时间已过，从未开始的直播内移除直播，listingtime不变 ");
		try {
			//创建一个立即上架的商品
			String productid = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			//创建一个未开始的直播
			Integer activityid = LiveCallService.createLiveBegin6dayInFuture(sellerName, sellerId).getJSONObject("Data").getInt("ActivityId");
			//加入商品到直播
			SellerProductCallService.addProductInLive(sellerId, activityid, productid);
			//移出直播
			Map<String, Object> updatemap = new HashMap<String, Object>();
			updatemap.put("ProductId", productid);
			updatemap.put("LiveId", activityid);
			updatemap.put("UserId", sellerId);
			JSONObject response = new RemoveProductFromLiveCall().callAndGetReturnData(updatemap);
			Logger.verifyEquals(200, response.getInt("Code"), "接口返回200");
			//验证listing
			SellerProductCallService.checkSqlListingTime(productid, 1);
			SellerProductCallService.checkListingTime(productid, null);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Logger.verifyEquals(0,new LiveProductsWapper().getCountByProductId(productid),"Mongo-LiveProducts 移出直播后，直播商品记录删除");
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productid);
				NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(activityid);
			
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RemoveProductFromLive_003() {
		Logger.start(true,"校验首次露出时间 - litingtime已过，加入正在进行中的直播，移除直播后，listingtime不变");
		try {
			//创建一个正在进中的直播
			Integer activityid = LiveCallService.createLiveInProgress(sellerName, sellerId).getJSONObject("Data").getInt("ActivityId");
			//创建一个定时上架的商品并且加入进行中的直播
			List list = new ArrayList<>();
			list.add(activityid);
			Map<String, Object> updatemap = new HashMap<String, Object>();
			updatemap.put("Product->LiveIdList", new JSONArray(list));
			String productid = SellerProductCallService.addNewProductOnTime(sellerId, sellerName, updatemap).getJSONObject("Data").getString("ProductID");

			//移出直播
			Map<String, Object> updatemap1 = new HashMap<String, Object>();
			updatemap1.put("ProductId", productid);
			updatemap1.put("LiveId", activityid);
			updatemap1.put("UserId", sellerId);
			JSONObject response = new RemoveProductFromLiveCall().callAndGetReturnData(updatemap1);
			Logger.verifyEquals(200, response.getInt("Code"), "接口返回200");
			//验证listing
			SellerProductCallService.checkSqlListingTime(productid, 3);
			SellerProductCallService.checkListingTime(productid, null);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productid);
				NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(activityid);
				Logger.verifyEquals(0,new LiveProductsWapper().getCountByProductId(productid),"Mongo-LiveProducts 移出直播后，直播商品记录删除");
				
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RemoveProductFromLive_004() {
		Logger.start(true,"校验首次露出时间 - 定时上架，在未开始的直播内，移除直播后，listingtime为定时上架的时间");
		try {
			//创建一个正在进中的直播
			Integer activityid = LiveCallService.createLiveBeginInFuture(sellerName, sellerId).getJSONObject("Data").getInt("ActivityId");
			//创建一个定时上架的商品
			String productid = SellerProductCallService.addNewProductOnTime(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			//加入商品到直播
			SellerProductCallService.addProductInLive(sellerId, activityid, productid);
			
			YMTDateUtil.waitTime(1);
			
			//移出直播
			Map<String, Object> updatemap1 = new HashMap<String, Object>();
			updatemap1.put("ProductId", productid);
			updatemap1.put("LiveId", activityid);
			updatemap1.put("UserId", sellerId);
			JSONObject response = new RemoveProductFromLiveCall().callAndGetReturnData(updatemap1);
			Logger.verifyEquals(200, response.getInt("Code"), "接口返回200");
			
			//验证listing
			SellerProductCallService.checkSqlListingTime(productid, 2);
			SellerProductCallService.checkListingTime(productid, null);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productid);
				NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(activityid);
				Logger.verifyEquals(0,new LiveProductsWapper().getCountByProductId(productid),"Mongo-LiveProducts 移出直播后，直播商品记录删除");
				
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RemoveProductFromLive_005() {
		Logger.start(true,"校验首次露出时间 -不上架商品，在未开始的直播内，移除直播后，listingtime为2099");
		try {
			//创建一个正在进中的直播
			Integer activityid = LiveCallService.createLiveBeginInFuture(sellerName, sellerId).getJSONObject("Data").getInt("ActivityId");
			//创建一个不上架的商品
			String productid = SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			//加入商品到直播
			SellerProductCallService.addProductInLive(sellerId, activityid, productid);
			
			YMTDateUtil.waitTime(1);
			
			//移出直播
			Map<String, Object> updatemap1 = new HashMap<String, Object>();
			updatemap1.put("ProductId", productid);
			updatemap1.put("LiveId", activityid);
			updatemap1.put("UserId", sellerId);
			JSONObject response = new RemoveProductFromLiveCall().callAndGetReturnData(updatemap1);
			Logger.verifyEquals(200, response.getInt("Code"), "接口返回200");
			//验证listing
			SellerProductCallService.checkSqlListingTime(productid, 4);
			SellerProductCallService.checkListingTime(productid, null);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productid);
				NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(activityid);
				Logger.verifyEquals(0,new LiveProductsWapper().getCountByProductId(productid),"Mongo-LiveProducts 移出直播后，直播商品记录删除");
				
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RemoveProductFromLive_006() {
		Logger.start(true,"校验首次露出时间 -直播A<直播B<直播C<商品上架时间，移除直播A，litingtime为直播B");
		try {

			//创建多场未开始的直播
			List<Integer> list = LiveCallService.createLivesInFuture(sellerId, sellerName, 3, 1);
			//创建一个5天后上架的商品
			String productid = SellerProductCallService.addNewProductOnTime(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			//加入到直播
			List list1 = new ArrayList<>();
			list1.addAll(list);
			Map<String, Object> updatemap = new HashMap<String, Object>();
			updatemap.put("SellerId", sellerId);
			updatemap.put("LiveIdList",new JSONArray(list1));
			updatemap.put("ProductId", productid);
			//组装修改后的参数并call接口
			AttachProductToLivesCall producttolives = new AttachProductToLivesCall();
			producttolives.callAndGetReturnData(updatemap);
			
			YMTDateUtil.waitTime(1);
			
			//移除直播
			Map<String, Object> updatemap1 = new HashMap<String, Object>();
			updatemap1.put("ProductId", productid);
			updatemap1.put("LiveId", list.get(0));
			updatemap1.put("UserId", sellerId);
			JSONObject response = new RemoveProductFromLiveCall().callAndGetReturnData(updatemap1);
			Logger.verifyEquals(200, response.getInt("Code"), "接口返回200");
			//验证listing
			SellerProductCallService.checkSqlListingTime(productid, 3);
			SellerProductCallService.checkListingTime(productid, null);
			
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productid);
				NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(list.get(0));
				Logger.verifyEquals(0,new LiveProductsWapper().getCountByLiveId(list.get(0)),"Mongo-LiveProducts 移出直播后，直播商品记录删除");
				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RemoveProductFromLive_007() {
		Logger.start(true,"校验首次露出时间 -商品上架时间<直播A<直播B<直播C<直播D，移除直播A，litingtime为商品上架时间");
		try {

			//创建多场未开始的直播
			List<Integer> list = LiveCallService.createLivesInFuture(sellerId, sellerName, 3, 6);
			//创建一个5天后上架的商品
			String productid = SellerProductCallService.addNewProductOnTime(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			//加入到直播
			List list1 = new ArrayList<>();
			list1.addAll(list);
			Map<String, Object> updatemap = new HashMap<String, Object>();
			updatemap.put("SellerId", sellerId);
			updatemap.put("LiveIdList",new JSONArray(list1));
			updatemap.put("ProductId", productid);
			//组装修改后的参数并call接口
			AttachProductToLivesCall producttolives = new AttachProductToLivesCall();
			producttolives.callAndGetReturnData(updatemap);
			//移除直播
			Map<String, Object> updatemap1 = new HashMap<String, Object>();
			updatemap1.put("ProductId", productid);
			updatemap1.put("LiveId", list.get(0));
			updatemap1.put("UserId", sellerId);
			JSONObject response = new RemoveProductFromLiveCall().callAndGetReturnData(updatemap1);
			Logger.verifyEquals(200, response.getInt("Code"), "接口返回200");
			//验证listing
			SellerProductCallService.checkSqlListingTime(productid, 2);
			SellerProductCallService.checkListingTime(productid, null);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productid);
				NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(list.get(0));
				Logger.verifyEquals(0,new LiveProductsWapper().getCountByLiveId(list.get(0)),"Mongo-LiveProducts 移出直播后，直播商品记录删除");
				
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RemoveProductFromLive_008() {
		Logger.start(true,"移除直播后，再次加入这个直播");
		try {
			//创建一个正在进中的直播
			Integer activityid = LiveCallService.createLiveInProgress(sellerName, sellerId).getJSONObject("Data").getInt("ActivityId");
			//创建一个定时上架的商品并且加入进行中的直播
			List list = new ArrayList<>();
			list.add(activityid);
			Map<String, Object> updatemap = new HashMap<String, Object>();
			updatemap.put("Product->LiveIdList", new JSONArray(list));
			String productid = SellerProductCallService.addNewProductOnTime(sellerId, sellerName, updatemap).getJSONObject("Data").getString("ProductID");

			//移出直播
			Map<String, Object> updatemap1 = new HashMap<String, Object>();
			updatemap1.put("ProductId", productid);
			updatemap1.put("LiveId", activityid);
			updatemap1.put("UserId", sellerId);
			JSONObject response = new RemoveProductFromLiveCall().callAndGetReturnData(updatemap1);
			Logger.verifyEquals(200, response.getInt("Code"), "接口返回200");
			
			YMTDateUtil.waitTime(2);
			
			//移除直播后check
			LiveProductModelWapper liveProductModelWapper = new LiveProductModelWapper();
			Logger.verifyIsNull(liveProductModelWapper.getByProductId(productid), "移除直播后，Mongo商品直播表应为空");
			
			//再次加入直播
	    	JSONObject addProductInLiveBean = SellerProductCallService.addProductInLive(sellerId,activityid,productid);
			
			//check SQL
			YmtProductsInLive ymtProductsInLive = new YmtProductsInLiveWapper().selectByLiveId(activityid).get(0);
			
			Map<String, Object> sqlTgtMap = new HashMap<String, Object>();
			sqlTgtMap.put("iuserid", sellerId); 
			sqlTgtMap.put("sproductid", productid); 
			sqlTgtMap.put("iactivityid", activityid); 
			sqlTgtMap.put("iAction", 0); 
			
			AssertService.assertResultEqual(sqlTgtMap, MapUtil.pojoToMap(ymtProductsInLive));	    	
			
			//check Mongo
			//Logger.verifyNotNull(liveProductModelWapper.getByProductId(productid), "重新加入直播后，Mongo商品直播表应不为空");
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productid);
				Logger.verifyEquals(1,new LiveProductsWapper().getCountByProductId(productid),"Mongo-LiveProducts 移出直播后，直播商品记录删除");
				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RemoveProductFromLive_009() {
		Logger.start(true,"移除直播后，删除商品");
		try {
			//创建一个正在进中的直播
			Integer activityid = LiveCallService.createLiveInProgress(sellerName, sellerId).getJSONObject("Data").getInt("ActivityId");
			//创建一个定时上架的商品并且加入进行中的直播
			List list = new ArrayList<>();
			list.add(activityid);
			Map<String, Object> updatemap = new HashMap<String, Object>();
			updatemap.put("Product->LiveIdList", new JSONArray(list));
			String productid = SellerProductCallService.addNewProductOnTime(sellerId, sellerName, updatemap).getJSONObject("Data").getString("ProductID");

			//移出直播
			Map<String, Object> updatemap1 = new HashMap<String, Object>();
			updatemap1.put("ProductId", productid);
			updatemap1.put("LiveId", activityid);
			updatemap1.put("UserId", sellerId);
			JSONObject response = new RemoveProductFromLiveCall().callAndGetReturnData(updatemap1);
			Logger.verifyEquals(200, response.getInt("Code"), "接口返回200");
			
			YMTDateUtil.waitTime(2);
			
			//移除直播后check
			LiveProductModelWapper liveProductModelWapper = new LiveProductModelWapper();
			Logger.verifyIsNull(liveProductModelWapper.getByProductId(productid), "移除直播后，Mongo商品直播表应为空");
			
			//批量删除商品
			List<String> deletelist=new ArrayList<String>();
			deletelist.add(productid);
			
			HashMap<String, Object> calldate = new HashMap<String, Object>();
			calldate.put("ProductIds",new JSONArray(list));
			calldate.put("UserID",sellerId);
			JSONObject result = new BatchDeleteProductsCall().callAndGetReturnData(calldate);
						
			//check SQL
			YmtProductsInLive ymtProductsInLive = new YmtProductsInLiveWapper().selectByLiveId(activityid).get(0);
			
			Map<String, Object> sqlTgtMap = new HashMap<String, Object>();
			sqlTgtMap.put("iuserid", sellerId); 
			sqlTgtMap.put("sproductid", productid); 
			sqlTgtMap.put("iactivityid", activityid); 
			sqlTgtMap.put("iAction", -1); 
			
			AssertService.assertResultEqual(sqlTgtMap, MapUtil.pojoToMap(ymtProductsInLive));	    	
			
			//check Mongo
			Logger.verifyIsNull(liveProductModelWapper.getByProductId(productid), "Mongo直播表应为空");
			
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				Logger.verifyEquals(0,new LiveProductsWapper().getCountByProductId(productid),"Mongo-LiveProducts 移出直播后，直播商品记录删除");
				
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	

}













